<template>
  <div class="centered-content">
    <div v-if="show" class="lose_efficacy">
      <p>该二维码已失效</p>
      <p @click="goBack">返回</p>
    </div>
    <div class="join_member" v-else>
      <div>
        <p class="secend">您当前在{{enterpriseName}}会员等级为<span>{{userInfo.gradeName}}</span></p>
        <p class="secend">是否加入会员等级<span>{{formatterType(Number(gradeCode))}}</span>？</p>
      </div>
      <div class="btn_module">
        <p @click="goBack">返回</p>
        <p @click="confirmJoin">确认加入</p>
      </div>
    </div>
  </div>
</template>

<script>
import { Overlay, Icon, Dialog, Toast } from "vant";
import { mapGetters} from "vuex";
import Vue from "vue";
Vue.use(Toast);
export default {
  head() {
    return {
      title: "加入会员"
    };
  },
  data() {
    return {
      version: "",
      show:false,
      gradeCode:'',
      enterpriseName:'',
      info:''
    };
  },
  mounted() {
    this.init();
    console.log('userInfo',this.userInfo);
  },
  computed:{
    ...mapGetters({
      userInfo: "authen/userInfo"
    }),
  },
  methods: {
    goBack() {
      console.log('链接',window.location.origin+`/${this.enterpriseId}/my`);
      window.location.href=window.location.origin+`/${this.enterpriseId}/my`
    },
    async confirmJoin() {
      const res=await this.$axios.$get("/gradeQrSetting/join",{
        params:{ 
          enterpriseId:this.enterpriseId,
          gradeCode:this.gradeCode,
          version :this.version,
          userId:this.userInfo.userId
        }
      })
      if(res&&res.retCode==0){ 
        this.$router.replace({
        path: `/${this.enterpriseId}/joinmember/joinsucess`,
        query:{ 
          data:JSON.stringify(res.data)
        }
      })
      }else{ 
        this.$toast.fail(res&&res.msg)
      }
    },
    async init() {
      // 在请求发送前检查 this.version 是否已经被赋值
      if (this.version !== null) {
        const res = await this.$axios.$get("/gradeQrSetting/query", {
          params: {
            enterpriseId: this.enterpriseId,
            version: this.version,
            gradeCode: this.gradeCode
          }
        });
        // 处理 res 的逻辑
        if(res&&res.retCode==0){ 
          if(res.data==null){ 
            this.show=true
          }else{ 
            this.info=res.data||''
            this.enterpriseName=res.data.enterpriseName||''
          }
        }
      } else {
        console.error("this.version is null. Ensure it has been correctly assigned.");
      }
    },
    // 格式化用户类型
    formatterType(gradeCode) {
      const types = {
        1: "普卡",
        2: "银卡",
        3: "金卡",
        4: "钻卡",
        5: "黑卡",
        s: "超级会员"
      };
      return types[gradeCode] || "";
    },
    // 格式化下一个用户等级
    formatterNextUserLevel() {
      const types = {
        1: "普卡",
        2: "银卡",
        3: "金卡",
        4: "钻卡",
        5: "黑卡"
      };
      let gradeCodeNum = this.gradeCode;
      let gradeName = "";
      if (gradeCodeNum === "s" || gradeCodeNum === "5") {
        gradeName = "";
      } else {
        gradeCodeNum = Number(gradeCodeNum);
        gradeName = types[gradeCodeNum];
      }
      return gradeName;
    },
  },
  beforeRouteEnter(to, from, next) {
    const versionValue = to.query.version;
    const gradeCode=to.query.gradeCode
    console.log('versionValue',versionValue);
    console.log('gradeCode',gradeCode);
    next(vm => {
      vm.version = versionValue;
      vm.gradeCode = gradeCode;
    });
  }
};
</script>

<style lang="scss" scoped>
.centered-content {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: auto;
  p:first-child{ 
    font-size: 15px;

  }
  .lose_efficacy{ 
    width: 100%;
    font-size: 14px;
    color: #333;
    text-align: center;
    position: relative;
    p:last-child {
      position: absolute;
      color: #333;
      font-size: 14px;
      background-color: #35b22a;
      color: #fff;
      text-align: center;
      border-radius: 6px;
      margin-top: 100px;
      left: 16px;
      right: 16px;
      padding: 10px 0;
    }
  }
  .join_member {
    text-align: center;
    font-size: 14px;
    .secend span{ 
      color: #7083ff;
      font-size: 15px;
    }
  }
  .btn_module {
    display: flex;
    justify-content: space-between;
    p:first-child {
      color: #333;
      font-size: 14px;
      border: 1px solid #666;
      padding: 7.5px 0;
      width: 90px;
      border-radius: 6px;
    }
    p:last-child {
      margin-left: 20px;
      color: #333;
      font-size: 14px;
      padding: 7.5px 0;
      width: 90px;
      background-color: #35b22a;
      color: #fff;
      border-radius: 6px;
    }
  }
}
</style>
